﻿import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import FilUI

ProgressBar {
    id: control

    property int duration                       : 888
    property real strokeWidth                   : 6
    property bool progressVisible               : false
    property color color                        : FilTheme.primaryColor
    property color backgroundColor              : Qt.rgba(214/255, 214/255, 214/255, 1)

    indeterminate : true
    background                                  : Rectangle {
        implicitWidth                           : 150
        implicitHeight                          : control.strokeWidth
        color                                   : control.backgroundColor
        radius                                  : d._radius
    }

    QtObject{
        id: d
        property real _radius                   : strokeWidth/2
    }

    contentItem                                 : FilClip {
        clip                                    : true
        radius                                  : [d._radius, d._radius, d._radius, d._radius]
        Rectangle {
            id: rect_progress
            width                               : {
                if (control.indeterminate) {
                    return 0.5 * parent.width
                }
                return control.visualPosition * parent.width
            }
            height                              : parent.height
            radius                              : d._radius
            color                               : control.color
            SequentialAnimation on x {
                id: animator_x
                running                         : control.indeterminate && control.visible
                loops                           : Animation.Infinite
                onRunningChanged                : {
                    if (!running) {
                        rect_progress.x = 0
                    }
                }
                PropertyAnimation {
                    from                        : -rect_progress.width
                    to                          : control.width + rect_progress.width
                    duration                    : control.duration
                }
            }
        }
    }

    FilText {
        text                                    : (control.visualPosition * 100).toFixed(0) + "%"
        visible                                 : {
            if (control.indeterminate) {
                return false
            }
            return control.progressVisible
        }
        anchors {
            left                                : parent.left
            leftMargin                          : control.width+5
            verticalCenter                      : parent.verticalCenter
        }
    }
}
